package com.viewpagerindicator.sample.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.viewpagerindicator.sample.GoodsDetailActivity;
import com.viewpagerindicator.sample.R;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by zhao on 2015/11/8.
 */
public class GoodsItemAdapter extends ArrayAdapter<JSONObject> {
    private int resource;
    private LayoutInflater inflater;
    public GoodsItemAdapter(Context context, int resource, List<JSONObject> objects) {
        super(context, resource, objects);
        this.resource=resource;
        this.inflater=LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflater.inflate(resource, null);
        }
        JSONObject jsonObject=getItem(position);
        TextView itemNameTv= (TextView) convertView.findViewById(R.id.goodsitem_name);
        TextView itemDescTv= (TextView) convertView.findViewById(R.id.godsitem_desc);
        Button btn2Print= (Button) convertView.findViewById(R.id.btn_to_print);
        convertView.setTag(jsonObject);
        btn2Print.setTag(jsonObject);
        btn2Print.setOnClickListener((GoodsDetailActivity)getContext());
        try{
            convertView.setTag(jsonObject.getInt("id"));
            itemNameTv.setText(jsonObject.getString("name"));
            itemDescTv.setText(jsonObject.getString("desc"));
        }catch (Exception e){

        }
        return convertView;
    }
}
